package com.palm.platform.controller;

import com.palm.core.anno.Auth;
import com.palm.core.util.CollectionUtil;
import com.palm.core.util.StringUtil;
import com.palm.platform.domain.User;

import com.palm.platform.domain.UserRole;
import com.palm.platform.service.RoleService;
import org.noear.solon.core.handle.Result;
import com.palm.core.data.PageResult;
import org.noear.solon.annotation.*;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.Page;
import com.palm.platform.PlatformMenu;

import java.util.List;

/**
 * 用户
 * <p>
 * generated by palmboot
 **/
@Controller
@Mapping("user")
public class UserController {

    @Db
    SqlToyLazyDao dao;
    @Inject
    RoleService roleService;

    @Auth(PlatformMenu.USER_ADD)
    @Note("新增用户")
    @Mapping("add")
    @Tran
    public Result add(User user, List<String> roles) {
        String password = user.getPassword();
        if (StringUtil.isEmpty(password)) {
            password = "123456";
        }
        user.setPassword(StringUtil.md5Hex(user.getUsername() + "$" + password));
        dao.save(user);
        List<UserRole> addUserRoles = CollectionUtil.map(roles, role -> new UserRole(user.getUsername(), role));
        dao.saveAll(addUserRoles);
        return Result.succeed();
    }

    @Auth(PlatformMenu.USER_DELETE)
    @Note("删除用户")
    @Mapping("delete")
    @Tran
    public Result delete(List<User> users) {
        dao.deleteAll(users);
        //级联删除角色
        dao.executeSql("delete from t_user_role where user_name in (:users)", CollectionUtil.asMap("users", users));
        return Result.succeed();
    }

    @Auth(PlatformMenu.USER_EDIT)
    @Note("更新用户")
    @Post
    @Mapping("save")
    public Result save(User user, List<String> roles) {
        dao.update(user);
        //更新角色
        roleService.updateRolesOf(user.getUsername(), roles);
        return Result.succeed();
    }

    @Auth(PlatformMenu.USER)
    @Note("获取用户")
    @Mapping("detail")
    public Result<User> detail(User query) {
        User user = dao.load(query);
        if (user != null) {
            user.setRoles(roleService.getRoleOfUser(user.getUsername()));
        }
        return Result.succeed(user);
    }

    @Auth(PlatformMenu.USER)
    @Note("获取用户列表")
    @Mapping("list")
    public Result<List<User>> list(User query, Page page) {
        Page<User> result = dao.findPageBySql(page, "user_list", query);
        return PageResult.succeed(result);
    }

    @Auth(PlatformMenu.USER)
    @Mapping("alterPassword")
    public Result alterPassword(User user) {
        if (StringUtil.isEmpty(user.getUsername())) {
            return Result.failure("用户名不能为空");
        }
        dao.executeSql("update t_user set password=md5(concat(username,'$',:password)) where username=:username", user);
        return Result.succeed();
    }

    @Auth(PlatformMenu.USER)
    @Mapping("switchStatus")
    public Result switchStatus(User user) {
        if (StringUtil.isEmpty(user.getUsername())) {
            return Result.failure("用户名不能为空");
        }
        dao.executeSql("update t_user set enable=:enable where username=:username", user);
        return Result.succeed();
    }
}
